home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / pi-nredir.c < prev    next >
C/C++ Source or Header  |  1997-08-10  |  2KB  |  109 lines

  1. /* pi-nredir.c: Redirect a connection over the network
  2.  *
  3.  * Copyright (C) 1997, Kenneth Albanowski
  4.  *
  5.  * This is free software, licensed under the GNU Public License V2.
  6.  * See the file COPYING for details.
  7.  */
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "pi-source.h"
  13. #include "pi-socket.h"
  14. #include "pi-dlp.h"
  15.  
  16. #ifdef __EMX__
  17. #include <netinet/in.h>
  18. #endif
  19.  
  20. int main(int argc, char *argv[])
  21. {
  22.   struct sockaddr_in addr2;
  23.   struct pi_sockaddr addr;
  24.   int sd, sd2;
  25.   struct NetSyncInfo N;
  26.   char buffer[0xffff];
  27.   int len;
  28.   int ret;
  29.  
  30.   if (argc < 2) {
  31.     fprintf(stderr,"usage:%s %s\n",argv[0],TTYPrompt);
  32.     exit(2);
  33.   }
  34.  
  35.   if (!(sd = pi_socket(PI_AF_SLP, PI_SOCK_STREAM, PI_PF_PADP))) {
  36.     perror("pi_socket");
  37.     exit(1);
  38.   }
  39.     
  40.   addr.pi_family = PI_AF_SLP;
  41.   strcpy(addr.pi_device,argv[1]);
  42.   
  43.   ret = pi_bind(sd, (struct sockaddr*)&addr, sizeof(addr));
  44.   if(ret == -1) {
  45.     perror("pi_bind");
  46.     exit(1);
  47.   }
  48.  
  49.   ret = pi_listen(sd, 1);
  50.   if(ret == -1) {
  51.     perror("pi_listen");
  52.     exit(1);
  53.   }
  54.  
  55.   sd = pi_accept(sd, 0, 0);
  56.   if(sd < 0) {
  57.     perror("pi_accept");
  58.     exit(1);
  59.   }
  60.   
  61.   if (dlp_ReadNetSyncInfo(sd, &N) < 0) {
  62.     fprintf(stderr, "Unable to read network information, cancelling sync.\n");
  63.     exit(1);
  64.   }
  65.     
  66.   if (!N.lanSync) {
  67.     fprintf(stderr, "LAN Sync not enabled on your PalmPilot, cancelling sync.\n");
  68.     exit(1);
  69.   }
  70.  
  71.   putenv("PILOTLOGFILE=PiDebugNet.log");
  72.   
  73.   sd2 = pi_socket(AF_INET, PI_SOCK_STREAM, 0);
  74.   if (sd2 <0 ) {
  75.     perror("Unable to get socket 2");
  76.     exit(1);
  77.   }
  78.   printf("Got socket 2\n");
  79.   
  80.   memset(&addr2, 0, sizeof(addr2));
  81.   addr2.sin_family = AF_INET;
  82.   addr2.sin_port = htons(14238);
  83.  
  84.   if ((addr2.sin_addr.s_addr = inet_addr(N.hostAddress))==-1) {
  85.     fprintf(stderr, "Unable to parse PC address '%s'\n", N.hostAddress);
  86.     exit(1);
  87.   }
  88.   
  89.   ret = pi_connect(sd2, (struct sockaddr*)&addr2, sizeof(addr2));
  90.  
  91.   if (ret<0) {
  92.     perror("Unable to connect to PC");
  93.     exit(1);
  94.   }
  95.   
  96.   while ((len = pi_read(sd2, buffer, 0xffff))>0) {
  97.     pi_write(sd, buffer, len);
  98.     len = pi_read(sd, buffer, 0xffff);
  99.     if (len < 0)
  100.       break;
  101.     pi_write(sd2, buffer, len);
  102.   }
  103.   
  104.   dlp_AbortSync(sd);
  105.   close(sd2);
  106.  
  107.   exit(0);
  108. }
  109.